package com.ht.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ht.po.SysUser;
import com.ht.service.ISysUserService;

/**
 * 
 * 用户服务
 * @author Administrator
 *
 */
@Controller
@RequestMapping("user")
public class SysUserController {
	
	@Autowired
	private ISysUserService sysUserService;
	
	// 登录
	@RequestMapping("login")
	public String login(SysUser user, Model model, HttpServletRequest request) throws Exception {
		// 判断该用户是否被删除
		// 判断该用户是否被锁定
		
		String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
		//根据shiro返回的异常类路径判断，抛出指定异常信息
		if(exceptionClassName!=null){
			if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
				//最终会抛给异常处理器
				model.addAttribute("message", "账号不存在");
			} else if (IncorrectCredentialsException.class.getName().equals(
					exceptionClassName)) {
				model.addAttribute("message", "用户名/密码错误");
			}else {
				model.addAttribute("message", "未知错误");
				throw new Exception();//最终在异常处理器生成未知错误
			}
		}
		return "login";
	}
	
	// 查询用户列表
	@RequestMapping("delete")
	@RequiresPermissions("user:list")
	public String delete() {
		System.out.println("我查询了用户列表.....");
		return "login";
	}
	
	// 注册提交
	@RequestMapping("register")
	public String register(SysUser user) {
		// 注册需要判断该登录账号是否存在
		sysUserService.registerUser(user);
		return "register";
	}
}
